package com.zzxx.photograph.web.servlet;

import com.zzxx.photograph.domain.Img;
import com.zzxx.photograph.domain.PageBean;
import com.zzxx.photograph.service.ImgService;
import com.zzxx.photograph.service.impl.ImgServiceImpl;
import com.zzxx.photograph.web.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.Map;

@WebServlet("/search/*")
public class SearchServlet extends BaseServlet {
    private ImgService imgService=new ImgServiceImpl();
    public void searchImg(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        Map<String, String[]> condition = req.getParameterMap();

        // pageSize 默认值
        int pageSize = 16;
        String _pageSize = req.getParameter("pageSize");
        if (_pageSize != null && _pageSize.length() > 0) {
            pageSize = Integer.parseInt(_pageSize);
        }
        // currentPage 默认值
        int currentPage = 1;
        String _currentPage = req.getParameter("currentPage");
        if (_currentPage != null && _currentPage.length() > 0) {
            currentPage = Integer.parseInt(_currentPage);
        }
        PageBean<Img> pb = imgService.findImgsByIname(condition,currentPage,pageSize);
        writeValue(resp, pb);
    }

    public void findImg(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        req.setCharacterEncoding("GBK");
//        String iname = new String(req.getParameter("iname").getBytes("ISO-8859-1"),"utf-8");
//        System.out.println(iname);
//        String s = req.getCharacterEncoding();
//        System.out.println(s);
        String iname = URLDecoder.decode(req.getParameter("iname"),"utf-8");
//        iname = URLDecoder.decode(iname,"utf-8");
//        iname = URLDecoder.decode(iname,"utf-8");
//        System.out.println(iname);
//        Map<String, String[]> condition = req.getParameterMap();
//        String iname = req.getParameter("iname");
//        iname = "可爱";
//        System.out.println(condition);
        // pageSize 默认值
        int pageSize = 16;
        String _pageSize = req.getParameter("pageSize");
        if (_pageSize != null && _pageSize.length() > 0) {
            pageSize = Integer.parseInt(_pageSize);
        }
        // currentPage 默认值
        int currentPage = 1;
        String _currentPage = req.getParameter("currentPage");
        if (_currentPage != null && _currentPage.length() > 0) {
            currentPage = Integer.parseInt(_currentPage);
        }
        PageBean<Img> pb = imgService.findImgByIname(iname,currentPage,pageSize);
        writeValue(resp, pb);
    }
}
